//判断当前浏览器是否支持WebSocket
var item2 = localStorage.getItem("item");
var userInfoObj = JSON.parse(item2);
var userInfoId = userInfoObj.userId;
websocket = new WebSocket("ws://"+webSocketUrl+"/MyWebSocket/"+userInfoId);
//连接发生错误的回调方法
websocket.onerror = function () {
    console.log("连接失败");
};

//连接成功建立的回调方法
websocket.onopen = function () {
    console.log("连接成功");
}

//接收到消息的回调方法
websocket.onmessage = function (event) {
    console.log(event,'onmessage');
    var data = $.parseJSON(event.data);
    var mum = data['socketCount'];
    var msg = data['socketList'];
    //发送人：socketUser     发送时间：socketData
    // 发送消息：socketMessage     未读消息数量：socketCount
    // 所有未读消息集合：socketList
    if(data != null){
        if(parseInt(mum)>0){
            $("#msgNum").show();
            $("#msgNum").text(parseInt(mum));
        }else{
            $("#msgNum").hide();
        }
    }
}

//连接关闭的回调方法
websocket.onclose = function () {
    
}

//监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
window.onbeforeunload = function () {
    closeWebSocket();
}
function setMessageInnerHTML(sendMessage) {
        // document.getElementById('message').innerHTML += sendMessage + '<br/>';
        console.log(sendMessage);
}
function closeWebSocket() {
        websocket.close();
}

function send(message) {
    	console.log(111111,'websocket——send');
        websocket.send(message);

} 
function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
        return currentdate;

}

    